package nl.sogyo.objectvisualisatie.GUI;

import java.awt.BorderLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import nl.sogyo.objectvisualisatie.core.Model;
import nl.sogyo.objectvisualisatie.data.MethodInfo;
import nl.sogyo.objectvisualisatie.data.ObjectInfo;


@SuppressWarnings("serial")
public class MethodListView extends JPanel implements Observer, ListSelectionListener, MouseListener {
	
	private JList methodList;
    private DefaultListModel methodListModel;
    private Model model;
	
    /**
     * Constructor in which the JList and ListModel will be created
     */
	public MethodListView()
	{
		super(new BorderLayout());
		methodListModel = new DefaultListModel();
        methodList = new JList(methodListModel);//create a JList
        methodList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        //methodList.setSelectedIndex(0);
        methodList.addListSelectionListener(this);
        methodList.setVisibleRowCount(10);
        methodList.addMouseListener(this);
       // methodList.setSize(200, 500);
        JScrollPane methodListScrollPane = new JScrollPane(methodList,
                JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		add(methodListScrollPane, BorderLayout.CENTER);	
		methodListModel.addElement("No methods executed yet");
	}

	
	public void valueChanged(ListSelectionEvent e)
	{
		// TODO Auto-generated method stub
	}
	
	/**
	 * Initialse the DataFacade of the application
	 * 
	 * @param dataFacade = DataFacade of the application
	 */
	public void initalise(Model model)
	{
		this.model = model;
	}
	
	/**
	 * Remove all elements form the listModel
	 */
	public void removeAllElements()
	{
		methodListModel.removeAllElements();
	}
	
	/**
	 * Specific update function of this class
	 */
	public void update()
	{
		if(model.getMethodcalls().size() == 0)
		{
			methodListModel.removeAllElements();
		}
		else if(model.getMethodcalls().size() != methodListModel.size())
		{
			MethodInfo methodInfo = model.getMethodcalls().get(model.getMethodcalls().size()-1);
			methodListModel.addElement(methodInfo.getFromObject().getClassName() +" || "+
										methodInfo.getMethod().name()+" || "+
										methodInfo.getOnObject().getClassName());
//			}
//			methodListModel.removeAllElements();
//			for(MethodInfo methodInfo : dataFacade.getMethodcalls()){
//				methodListModel.addElement(methodInfo.getFromObject().getClassName() +" || "+
//						methodInfo.getMethod().name()+" || "+
//						methodInfo.getOnObject().getClassName());
//			}
		}
	}

	public void mouseClicked(MouseEvent e) {
		for(ObjectInfo objectInfo : model.getobjectsList()){
			objectInfo.setSelected(false);		
		}
		MethodInfo info = (MethodInfo) model.getMethodcalls().get(methodList.locationToIndex(e.getPoint()));
		info.getFromObject().setSelected(true);
		model.notifyObs();
		info.getOnObject().setSelected(true);
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub		
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub		
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub		
	}
}
